package com.bangdream.michelia.view.fragment.currency;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bangdream.michelia.R;
import com.bangdream.michelia.application.MainApplication;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.sql.greendao.DaoSession;
import com.bangdream.michelia.sql.greendao.DbHelper;
import com.bangdream.michelia.sql.greendao.GreenDaoManager;
import com.bangdream.michelia.tool.StatusBarTool;
import com.bangdream.michelia.utils.L;
import com.bangdream.michelia.utils.PublicUtil;

/**
 * Created by Administrator on 2016/8/22.
 */
public abstract class BaseFragment<V, P extends BasePresenter<V>> extends Fragment {
    public TextView tv_left, tv_title, tv_right;
    public ProgressBar progressBar;
    private LinearLayout title_bar;
    private View statusBarHeight;
    public ProgressDialog pd;

    protected DbHelper dbHelper;
    protected DaoSession daoInstancs;
    protected MainApplication mApp;

    public void showText(String text) {
        Toast.makeText(MainApplication.getContext(), text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        getActivity().overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initConfig();
    }


    protected void initConfig() {
        if (dbHelper == null)
            dbHelper = DbHelper.getInstance();
        if (daoInstancs == null)
            daoInstancs = GreenDaoManager.getDaoInstant();
        if (pd == null && getActivity() != null) {
            pd = new ProgressDialog(getActivity());
        }
        if (mPresenter == null) {
            mPresenter = createPresenter();
        }
        if (mPresenter != null) {
            mPresenter.attach((V) this);
        }
    }

    /**
     * 初始化titlebar，该方法只有在标题栏布局符合此规则时才能调用
     *
     * @param left            titlebar左按钮
     * @param title           titlebar标题
     * @param right           titlebar 右按钮
     * @param onClickListener 左右按钮点击事件
     */
    public void initTitleBar(String left, String title, String right, View.OnClickListener onClickListener) {
        tv_left = (TextView) getView().findViewById(R.id.tv_left);//返回按钮
        tv_title = (TextView) getView().findViewById(R.id.tv_title);//标题
        tv_right = (TextView) getView().findViewById(R.id.tv_right);//更多(右侧)按钮
        progressBar = (ProgressBar) getView().findViewById(R.id.pb);// 标题栏数据加载ProgressBar

        if (!TextUtils.isEmpty(left)) {
            tv_left.setText(left);
            tv_left.setVisibility(View.VISIBLE);
            tv_left.setOnClickListener(onClickListener);
        }

        if (!TextUtils.isEmpty(title)) {
            tv_title.setText(title);
        }

        if (!TextUtils.isEmpty(right)) {
            tv_right.setText(right);
            tv_right.setVisibility(View.VISIBLE);
            tv_right.setOnClickListener(onClickListener);
        }
    }

    //透明状态栏的头部布局
    public void initTitleBar(String left, String title, String right, View.OnClickListener onClickListener, boolean isNoStatelessBar) {
        if (isNoStatelessBar) {
            title_bar = getView().findViewById(R.id.title_bar);
            statusBarHeight = getView().findViewById(R.id.statusBarHeight);
            ViewGroup.LayoutParams titleBarLayoutParams = title_bar.getLayoutParams();
            titleBarLayoutParams.height = PublicUtil.dip2px(getActivity(), 50) + StatusBarTool.getStatusHeight(getActivity());
            statusBarHeight.getLayoutParams().height = StatusBarTool.getStatusHeight(getActivity());
        }
        initTitleBar(left, title, right, onClickListener);
    }

    //MVP部分

    protected P mPresenter;

    public abstract P createPresenter();

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detach();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mApp=MainApplication.getInstance();
    }
}
